package com.example.web.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.utils.Result;
import com.example.web.system.entity.SaveRoleIdsByUserIdParam;
import com.example.web.system.entity.SysRole;
import com.example.web.system.entity.SysUser;
import com.example.web.system.entity.SysUserParam;
import com.example.web.system.service.ISysUserService;
import com.example.web.system.service.impl.SysRoleServiceImpl;
import com.example.web.system.service.impl.SysUserRoleServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kimido
 * @since 2024-06-27
 */
@RestController
@RequestMapping("/api/user")
@Api(tags = "用户管理")
public class SysUserController {//物主

    @Autowired
    private ISysUserService userService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private SysRoleServiceImpl roleService;

    @Autowired
    SysUserRoleServiceImpl sysUserRoleService;

    @ApiOperation("查询所有角色并选中用户拥有的角色")
    @GetMapping("/checkRoles/{userId}")
    public Result checkRoles(@PathVariable("userId") Integer userId){
        List<SysRole> list= roleService.checkRoles(userId);
        return Result.success(list);
    }

    @ApiOperation("保存用户拥有的角色信息")
    @PutMapping("/saveRoleIdsByUserId")
    public Result saveRoleIdsByUserId(@RequestBody SaveRoleIdsByUserIdParam param){
        boolean save =sysUserRoleService.saveRoleIdsByUserId(param);
        if(save){
            return Result.success();
        }
        return Result.error(500,"保存角色信息失败");
    }

    @ApiOperation("新增用户")
    @PostMapping("/add")
    public Result addUser(@RequestBody SysUser user){
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        user.setDeleted(1);
        user.setEnabled(true);
        user.setStatus(true);
        user.setAccountNonExpired(true);
        user.setAccountNonLocked(true);
        user.setCredentialsNonExpired(true);
        boolean save=userService.save(user);
        if(save){
            return Result.success();
        }
        return Result.error(500,"新增失败");
    }

    @ApiOperation("编辑用户")
    @PostMapping("/update")
    public Result editUser(@RequestBody SysUser user){

        boolean update=userService.updateById(user);
        if(update){
            return  Result.success();
        }
        return Result.error(500,"更新失败");
    }
    @ApiOperation("删除用户")
    @DeleteMapping("/delete/{userId}")
    public Result deleteUser(@PathVariable("userId") Integer userId){
        boolean remove=userService.removeById(userId);
        if(remove){
            return Result.success();
        }
        return Result.error(500,"删除失败");
    }
    @ApiOperation("用户列表")
    @GetMapping("/list")
    public Result list(SysUserParam sysUserParam){
        IPage<SysUser> page =userService.list(sysUserParam);
        return  Result.success(page);
    }

}
